package com.sanpu.kxe.business.main.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.business.main.bean.MaterialPosterOutput;
import com.sanpu.kxe.common.interfaces.UrlConstants;
import com.sanpu.kxe.jump.imageloader.JKImageLoader;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

/**
 * Description：推广海报
 * Created by 薛勇军 on 2019/9/28.
 */

public class MaterialPosterAdapter extends RecyclerView.Adapter<MaterialPosterAdapter.ViewHolder> {

    private Context mContext;

    private List<MaterialPosterOutput> mPosterOutputs = new ArrayList<>();

    public MaterialPosterAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setNotifyData(List<MaterialPosterOutput> posterOutputs) {
        this.mPosterOutputs = posterOutputs;
        this.notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_material_poster, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (mPosterOutputs != null && mPosterOutputs.size()>position) {
            MaterialPosterOutput output = mPosterOutputs.get(position);
            if (output != null) {
                JKImageLoader.getInstance().displayImage(mContext,
                        UrlConstants.getBaseUrl() + output.getBigImagePath(), holder.imageIv);
            }
        }
    }

    @Override
    public int getItemCount() {
        if (mPosterOutputs != null)
            return mPosterOutputs.size();
        else
            return 0;
    }

    public MaterialPosterOutput getItem(int position) {
        if (mPosterOutputs == null)
            return null;
        return mPosterOutputs.get(position);
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView imageIv;

        ViewHolder(View itemView) {
            super(itemView);
            imageIv = ButterKnife.findById(itemView, R.id.imageIv);
        }
    }
}
